package com.hqj.demo01;

import java.util.regex.Matcher;

/**
 *  * 有点类 Point ,属性有int x,int y。A(1,2)
 *  * x,y分别表示x轴 与y轴
 *  * 有构造方法对x,y进行初始化
 *  *
 *  * 有矩形类Rectangle,属性Point p,int w,int h
 *  * p表示中间点，w为宽,h为高
 *  * 有构造方法对属性进行初始化
 *  *
 *  * ①、有计算矩形面积的方法calcArea(),并返回结果；
 *  * ②、有判断某个点是否在矩形内的方法contains(Point point),
 *  * point即为某个点，
 *  * 判断这个点point是否在矩形内，在返回true,否则返回false
 *  *
 *  * 测试类
 *  * 调用calcArea(),输出面积
 *  * 假设某个点，调用contains(),输出结果(在或不在)
 */
public class Exercise01 {

    public static void main(String[] args) {
        Point p = new Point(1,2);
        int w = 10,h = 6;
        Rectangle r = new Rectangle(p,w,h);

        System.out.println("面积:"+r.calcArea());
        Point point = new Point(-3,-1);
        System.out.println("点"+(r.contains(point)?"在":"不在")+"矩形内");
    }

}

class Point{
    int x;
    int y;
    public Point(){}

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

class Rectangle{
    Point p;
    int w;
    int h;

    public Rectangle(){}

    public Rectangle(Point p, int w, int h) {
        this.p = p;
        this.w = w;
        this.h = h;
    }

    public long calcArea(){
        long area = w*h;
        return area;
    }

    public boolean contains(Point point){
        boolean flat = false;
        int x = Math.abs(point.x - p.x);
        int y = Math.abs(point.y - p.y);
        if(x<=w/2&&y<=h/2){
            flat = true;
        }
        return flat;
    }
}


